<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--企业数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="客户编码" prop="customerCode">
              <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="表号" prop="meterCode">
              <el-input v-model="queryParams.meterCode" placeholder="请输入表号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="手机号" prop="telephone">
              <el-input v-model="queryParams.telephone" placeholder="请输入手机号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="IMEI" prop="imei">
              <el-input v-model="queryParams.imei" placeholder="请输入IMEI" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="计价方案" prop="priceSchemeId">
              <el-select v-model="queryParams.priceSchemeId" filterable placeholder="请选择">
                <el-option :label="item.schemeName" :value="item.id" v-for="(item, index) in schemeList" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="小区名称" prop="areaName">
              <el-input v-model="queryParams.areaName" placeholder="请输入小区名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['fileManagement:civilArchivesQuery:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="postList" border @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="客户名称" align="center" prop="customerName" />
            <el-table-column label="客户编码" align="center" key="customerCode" prop="customerCode"
              :show-overflow-tooltip="true" />
            <el-table-column prop="status" label="客户类型" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.customer_type" :value="scope.row.customerType" />
              </template>
            </el-table-column>
            <el-table-column label="计价方案" align="center" key="priceSchemeName" prop="priceSchemeName"
                             :show-overflow-tooltip="true" width="150"/>
            <el-table-column label="小区" align="center" prop="areaName" width="120" />
            <el-table-column label="表号" align="center" prop="meterCode" width="120" />
            <el-table-column label="表计状态" align="center" prop="meterStatus" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.meterStatus == 1">待安装</span>
                <span v-if="scope.row.meterStatus == 2">已安装</span>
                <span v-if="scope.row.meterStatus == 3">已拆卸</span>
              </template>
            </el-table-column>
            <el-table-column label="激活状态" align="center" prop="activeStatus" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.activeStatus == 'N'">未激活</span>
                <span v-if="scope.row.activeStatus == 'Y'">已激活</span>
              </template>

            </el-table-column>
            <el-table-column label="阀门状态" align="center" prop="valveStatus" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.valveStatus == 0">关闭</span>
                <span v-if="scope.row.valveStatus == 1">开启</span>
                <span v-if="scope.row.valveStatus == 2">未知</span>
              </template>

            </el-table-column>


            <el-table-column label="供应商" align="center" prop="meterManufacturer" width="120" />
            <!--      <el-table-column label="基表手机号" align="center" prop="meterPhoneNum" width="120" />-->
            <el-table-column label="表计类型" align="center" prop="meterType" width="150" />
            <el-table-column label="证件号" align="center" key="idNumber" prop="idNumber" :show-overflow-tooltip="true"
              width="100" />
            <el-table-column label="联系电话" align="center" key="telephone" prop="telephone" :show-overflow-tooltip="true"
              width="120" />
            <el-table-column label="客户地址" align="center" key="customerAddress" prop="customerAddress"
              :show-overflow-tooltip="true" />
            <el-table-column label="气表进气方向" align="center" prop="intakeDirection" width="150">
              <template slot-scope="scope">
                <span v-if="scope.row.intakeDirection == 'L'">左进</span>
                <span v-if="scope.row.intakeDirection == 'R'">右进</span>
              </template>
            </el-table-column>
            <el-table-column label="ICCID" align="center" prop="iccid" />
            <el-table-column label="IMEI" align="center" prop="imei" />
            <el-table-column label="DTU编号" align="center" prop="dtuCode" />
            <!-- <el-table-column label="设备ID" align="center" prop="deviceId" /> -->
            <el-table-column label="流量计厂家" align="center" prop="flowmeterManufacturer" width="120" />
            <el-table-column label="基表底数" align="center" prop="baseNumber" />
            <el-table-column label="安装日期" align="center" prop="installTime" width="170" />

            <el-table-column label="报警额度" align="center" prop="alarmGas" />
            <el-table-column label="是否带流量计" align="center" prop="isFlowmeter" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isFlowmeter == 'N'">否</span>
                <span v-if="scope.row.isFlowmeter == 'Y'">是</span>
              </template>
            </el-table-column>
            <el-table-column label="流量计起始量" align="center" prop="initialGas" width="150" />
            <el-table-column label="是否带GPRS" align="center" prop="isGprs" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isGprs == 'N'">否</span>
                <span v-if="scope.row.isGprs == 'Y'">是</span>
              </template>
            </el-table-column>

            <el-table-column label="通讯地址" align="center" prop="mailAddress" />
            <el-table-column label="通讯协议" align="center" prop="mailProtocol" />
            <!--      <el-table-column label="工程工料费" align="center" prop="projectFee" width="120" />-->
            <!--      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
              <template slot-scope="scope">
                <el-button v-if="scope.row.meterStatus != 3" size="mini" type="text" icon="el-icon-edit"
                  @click="handleUpdate(scope.row)" v-hasPermi="['system:meterarchives:edit']">修改</el-button>
                <el-button v-if="scope.row.meterStatus != 3" size="mini" type="text" icon="el-icon-delete"
                  @click="handleDelete(scope.row)" v-hasPermi="['system:meterarchives:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>
        <el-card class="box-card" style="margin-top: 10px">
          <el-tabs v-model="activeName" @tab-click="handleClick">

            <el-tab-pane label="事件记录" name="3">
              <el-table v-loading="loading" :data="userList" border>
                <el-table-column label="类型名称" align="center" key="monitorTypeTitle" prop="monitorTypeTitle"
                  :show-overflow-tooltip="true" width="100" />
                <!-- <el-table-column label="类型编码" align="center" key="monitorTypeCode" prop="monitorTypeCode"
              :show-overflow-tooltip="true" width="100" /> -->
                <el-table-column label="客户名称" align="center" key="customerName" prop="customerName"
                  :show-overflow-tooltip="true" />
                <el-table-column label="客户编码" align="center" key="customerCode" prop="customerCode"
                  :show-overflow-tooltip="true" />
                <el-table-column label="手机号" align="center" key="telephone" prop="telephone"
                  :show-overflow-tooltip="true" />
                <el-table-column label="客户地址" align="center" key="customerAddress" prop="customerAddress"
                  :show-overflow-tooltip="true" />
                <!-- <el-table-column label="设备ID" align="center" key="meterId" prop="meterId" :show-overflow-tooltip="true" /> -->
                <el-table-column label="表号" align="center" key="meterCode" prop="meterCode"
                  :show-overflow-tooltip="true" />
                <el-table-column label="内部电压" align="center" key="inBattery" prop="inBattery"
                  :show-overflow-tooltip="true" />
                <el-table-column label="采集时间" align="center" key="gatherTime" prop="gatherTime"
                  :show-overflow-tooltip="true" />
                <el-table-column label="网络信息质量" align="center" key="networkSignalQuality" prop="networkSignalQuality"
                  :show-overflow-tooltip="true" width="150" />
                <el-table-column label="外部电池电压" align="center" key="outBattery" prop="outBattery"
                  :show-overflow-tooltip="true" width="150" />
                <el-table-column label="阀门状态" align="center" key="valveStatus" prop="valveStatus"
                  :show-overflow-tooltip="true" width="120">
                  <template slot-scope="scope">
                    <span v-if="scope.row.valveStatus == '0'">关闭</span>
                    <span v-if="scope.row.valveStatus == '1'">开启</span>
                    <span v-if="scope.row.valveStatus == '2'">未知</span>
                  </template>
                </el-table-column>
                <el-table-column label="状态" align="center" key="status" prop="status" :show-overflow-tooltip="true"
                  width="100">
                  <template slot-scope="scope">
                    <span v-if="scope.row.status == '0'">异常</span>
                    <span v-if="scope.row.status == '1'">正常</span>
                  </template>
                </el-table-column>
                <el-table-column label="激活状态" align="center" key="activeStatus" prop="activeStatus"
                  :show-overflow-tooltip="true" width="100">
                  <template slot-scope="scope">
                    <span v-if="scope.row.activeStatus == 'N'">未激活</span>
                    <span v-if="scope.row.activeStatus == 'Y'">已激活</span>
                  </template>
                </el-table-column>
              </el-table>
              <pagination v-show="eqtotal > 0" :total="eqtotal" :page.sync="queryParams2.pageNum"
                :limit.sync="queryParams2.pageSize" @pagination="eqgetList" />
            </el-tab-pane>
            <el-tab-pane label="开关阀记录" name="4">
              <el-table :data="tableData4" border style="width: 100%">
                <el-table-column type="index" width="80" prop="date" label="设备ID" align="center">
                </el-table-column>
                <el-table-column prop="instructName" label="指令名称" align="center"></el-table-column>
                <el-table-column prop="flowNo" label="流水号" align="center">
                </el-table-column>
                <!-- <el-table-column prop="instructContent" label="指令内容" align="center">
                </el-table-column> -->
                <el-table-column prop="instructType" label="指令类型" align="center">
                  <template slot-scope="scope">
                    <span v-if="scope.row.instructType == '00'">激活</span>
                    <span v-if="scope.row.instructType == '01'">关阀</span>
                    <span v-if="scope.row.instructType == '02'">开阀</span>
                    <span v-if="scope.row.instructType == '03'">调价</span>
                    <span v-if="scope.row.instructType == '04'">充值</span>
                    <span v-if="scope.row.instructType == '05'">扣费</span>
                    <span v-if="scope.row.instructType == '06'">冲正</span>
                    <span v-if="scope.row.instructType == '07'">余额同步</span>
                    <span v-if="scope.row.instructType == '08'">数据采集</span>
                  </template>
                </el-table-column>
                <el-table-column prop="mchNo" label="商户号" align="center">
                </el-table-column>
                <el-table-column prop="operUser" label="操作人" align="center">
                </el-table-column>
                <el-table-column prop="sendTime" label="发送时间" align="center">
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                  <template slot-scope="scope">
                    <span v-if="scope.row.status == 0">待执行</span>
                    <span v-if="scope.row.status == 1">已下发</span>
                    <span v-if="scope.row.status == 2">已完成</span>
                    <span v-if="scope.row.status == 3">已过期</span>
                  </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center">
                </el-table-column>
              </el-table>
              <pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams1.pageNum"
                :limit.sync="queryParams1.pageSize" @pagination="getInstructList" />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  deptTreeSelect,
} from "@/api/system/user";
import { meterarchivesList, meterarchivesInfo, addMeterarchives, updateMeterarchives, delMeterarchives, exportMeterarchives } from "@/api/fileManagement/meterarchives.js"
import { getToken } from "@/utils/auth";
import { instructList, } from "@/api/fileManagement/civilArchives.js"
import FreezeData from '../components/icons/lineEcharts.vue'
import { areaList, areaTreeList } from "@/api/fileManagement/areainfo.js"
import { monitorList } from "@/api/equipmentMonitoring/equipmentAlarm.js"
import { getGasmeterList } from '@/api/systemSetup/gasMeter.js'
import { getSupplierList } from '@/api/system/supplierInfo.js'
import { planSelectList } from '@/api/service/planlist'

export default {
  name: "TableArchivesQuery",
  components: { FreezeData },
  dicts: ["sys_normal_disable", "customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      eqtotal: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 企业树选项
      deptOptions: undefined,
      // 企业名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        imei: null,
        meterType: null,
      },
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "小区名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "小区名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        nickName: [
          { required: true, message: "小区昵称不能为空", trigger: "blur" },
        ],
      },
      activeName: "4",
      tableData: [{}],
      tableData2: [{}],
      tableData3: [{}],
      tableData4: [{}],
      tableData5: [{}],
      tableData6: [{}],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value2: "",
      radio: 1,
      value3: "",
      postList: [],
      meterList: [],
      supplierList: [],
      schemeList:[]
    };
  },
  watch: {
    // 根据名称筛选企业树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {

    this.getDeptTree();
    this.getMeterList()
    this.getsupplierList()
    this.getList();
    this.getPricingPlanList()
  },
  methods: {
    // 查询计价方案
    getPricingPlanList() {
      planSelectList().then((res) => {
        this.schemeList = res
      })
    },
    // 获取事件记录
    eqgetList() {
      this.loading = true;
      monitorList(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.eqtotal = response.total;
        this.loading = false;
      });
    },
    // 表格行点击
    rowClick(row) {
      this.queryParams1.customerId = row.customerId
      this.getInstructList()
    },

    handleSelectionChange() { },
    handleClick(tab, event) {
      console.log(tab.name, event);
      if (tab.name == 4) {
        this.getInstructList()
      } else if (tab.name == 3) {
        this.eqgetList()
      }

    },
    /** 查询供应商列表 */
    getsupplierList() {
      let params = {
        pageNum: 1,
        pageSize: 9999,
      }
      getSupplierList(params).then((response) => {
        this.supplierList = response.data;
      });
    },
    // 获取记录查询
    getInstructList() {
      instructList(this.queryParams1).then((res) => {
        this.tableData4 = res.rows
        this.total1 = res.total
      })
    },
    // 获取表计类型
    getMeterList() {
      getGasmeterList().then((res) => {
        this.meterList = res.data
      })
    },
    /** 查询小区列表 */
    getList() {
      this.loading = true;
      meterarchivesList(this.queryParams).then((response) => {
        if (response.code == 200) {
          let list = JSON.parse(JSON.stringify(response.rows))
          list.map((res) => {
            this.meterList.map((item) => {
              if (res.meterType == item.id) {
                res.meterType = item.typeName
              }
            })
            this.supplierList.map((res1) => {
              if (res.meterManufacturer == res1.id) {
                res.meterManufacturer = res1.supplierName
              }
            })
          })
          this.postList = list
          this.total = response.total;
          this.loading = false;
        }
      });
    },
    /** 查询企业下拉树结构 */
    getDeptTree() {
      areaTreeList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/meterarchives/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}

.tablex {
  width: 100%;
  border-collapse: collapse;
  border-color: #ebeef5;

  td {
    width: 25%;
    padding: 10px;
    color: #606266;
    font-size: 14px;
  }

  .lables {
    background: rgba(150, 154, 184, 0.05);
    text-align: right;
  }
}
</style>
